<template>
  <div class="interspace">
    <div class="main">
      <div class="fb" style="cursor: pointer" @click="fb">发布动态</div>
      <div v-if="total > 0">
        <div class="m_item" v-for="(item, index) in data" :key="item.id">
          <div class="it_top">
            <div class="p_lef">
              <div class="f_p">
                <div class="ri">{{ item.day }}</div>
                <div class="yue">{{ item.month }}月</div>
              </div>
              <div class="f_m">{{ item.time }}</div>
            </div>
            <div class="p_rig">
              <div v-if="ad === index">
                {{ item.content }}
              </div>
              <div class="text" v-else>
                {{ item.content }}
              </div>
              <div class="kai" v-show="item.content.length > 115">
                <div @click.stop="zhankai(index)">
                  {{ ad === index ? (sub = '收起') : (sub = '展开') }}
                </div>
              </div>
              <!-- <div class="zk">展开</div> -->
            </div>
          </div>
          <div class="it_img">
            <img :src="e" alt="" v-for="(e, i) in item.images" :key="i" />
          </div>
          <div class="it_bom">
            <div></div>
            <div class="m_le" style="cursor: pointer" @click="like(item.id)">
              <div class="szan" v-if="item.is_like == 0">
                <img src="@/assets/nozan.png" alt="" />
              </div>
              <div class="szan" v-else>
                <img src="@/assets/zan.png" alt="" />
              </div>
              <!-- <el-icon v-if="item.is_like == 0"><Pointer /></el-icon>
              <el-icon v-else color="#E02020"><Pointer /></el-icon> -->
              <div class="le">{{ item.like }}</div>
            </div>
            <div class="m_ri" style="cursor: pointer" @click="del(item.id)">
              <el-icon color="#E02020"><Delete /></el-icon>
            </div>
          </div>
          <div class="br"></div>
        </div>
      </div>
      <div class="rq" v-else>
        <img src="@/image/容器.svg" alt="" />
        <div>暂无信息</div>
      </div>
      <div class="con_bom" v-if="total > 0">
        <div class="cc">共{{ total }}条</div>
        <el-pagination
          v-model:page-size="pageSize"
          background
          layout="prev, pager, next"
          :total="total"
          @next-click="nextclick"
          @prev-click="prevclick"
          @change="change1"
        />
      </div>
    </div>
    <!-- 弹框 -->
    <div class="mask" v-if="centerDialogVisible">
      <div class="content" v-if="centerDialogVisible1">
        <div class="con_title">
          <div class="e_lef">发布动态</div>
          <!-- <div class="e_rig" style="cursor: pointer" @click="qu">x</div> -->
          <img
            class="e_rig"
            style="cursor: pointer"
            @click="qu"
            src="@/image/xxx.png"
            alt=""
          />
        </div>
        <el-input
          v-model="textarea"
          maxlength="1000"
          placeholder="请输入"
          show-word-limit
          type="textarea"
          @input="mychange"
        />
        <div class="img">
          <div v-for="(item, index) in imgs" class="bbb">
            <img
              src="@/image/xxx.png"
              class="aaa"
              style="cursor: pointer"
              @click="myxxx(index)"
            />
            <img :src="item" alt="" class="imgs" :key="item" />
          </div>
        </div>
        <div class="sc" v-if="imgs.length < 8">
          <div class="sc_lef">
            <el-icon><PictureFilled /></el-icon>
          </div>
          <div class="sc_rig">
            <el-upload
              :auto-upload="false"
              accept="image/*"
              :show-file-list="false"
              :on-change="avatarFileChange"
              :before-upload="beforeAvatarUpload"
            >
              <div class="dj">点击上传</div>
            </el-upload>
            <!-- <div class="dj">点击上传</div> -->
            <div class="w">
              限8张，图片格式限 png,bmp,jpg,jpeg; 图片大小小于1M
            </div>
          </div>
        </div>
        <!-- 发布 -->
        <div class="fw" v-if="position <= 2">
          <div class="fw_lef">发布范围</div>
          <div class="fw_rig">
            <el-radio-group v-model="data1.range" class="ml-4">
              <el-radio label="1" size="large">个人风采</el-radio>
              <el-radio label="2" size="large">支部风采</el-radio>
            </el-radio-group>
          </div>
        </div>
        <div class="btn">
          <div class="bt qx" style="cursor: pointer" @click="qu">取消</div>
          <div class="bt tj" style="cursor: pointer" @click="add">提交</div>
        </div>
      </div>
      <div class="content asd" v-else>
        <div class="con_title">
          <div class="e_lef"></div>
          <!-- <div class="e_rig" style="cursor: pointer" @click="center">x</div> -->
          <img
            class="e_rig"
            style="cursor: pointer"
            @click="qu"
            src="@/image/xxx.png"
            alt=""
          />
        </div>
        <div class="con">
          <div class="tit">动态已提交，等待系统审核后展示</div>
          <div class="wzdl" style="cursor: pointer" @click="center">
            我知道了
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import {
  getlistsApi,
  getlikeApi,
  gerdelApi,
  reqUpload,
  getaddApi
} from '@/api/user'

import type { UploadProps } from 'element-plus'
const textarea = ref('')
const mychange = () => {
  if (textarea.value.length > 1000) {
    textarea.value = data1.value.content
    ElMessage.success('内容需在1000字内')
  } else {
    data1.value.content = textarea.value
  }
  // console.log(data1.value.center.length)
}
let sub = ref('展开')
let ad = ref(null)
const zhankai = (index: any) => {
  if (ad.value === index) {
    ad.value = null // 点击已展开的内容时收起
  } else {
    ad.value = index // 点击未展开的内容时展开
  }
}
const position: any = localStorage.getItem('position')
let data1: any = ref({
  content: '',
  range: '1',
  images: ''
})
const myxxx = (i: any) => {
  imgs.value.splice(i, 1)
  console.log(imgs.value.length, '迟到')
}
const fb = () => {
  centerDialogVisible.value = true
  // console.log(position)
}
const add = () => {
  // data1.value.images = JSON.stringify(imgs.value)
  data1.value.images = imgs.value.join(',')
  console.log(data1.value.images, '图片')
  getaddApi(data1.value).then(res => {
    console.log(res, '提交')
    textarea.value = ''
    imgs.value = []
    data1.value = {
      content: '',
      range: '1',
      images: ''
    }
    ElMessage.success(res.data.msg)
    if (position <= 2) {
      centerDialogVisible1.value = true
      centerDialogVisible.value = false
    } else {
      centerDialogVisible1.value = false
    }

    getlists()
  })
}

const qu = () => {
  textarea.value = ''
  imgs.value = []
  data1.value = {
    content: '',
    range: '1',
    images: ''
  }
  centerDialogVisible.value = false
  centerDialogVisible1.value = true
}
const avatarFile = ref({})
const imageUrl: any = ref('')
const imgs: any = ref([])
function dataURLtoFile(dataURL: any) {
  let arr = dataURL.split(',')
  let mime = arr[0].match(/:(.*?);/)[1]
  let bstr = atob(arr[1])
  let n = bstr.length
  let u8arr = new Uint8Array(n)
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n)
  }
  return new File([u8arr], `${Date.now}.jpg`, {
    type: mime
  })
}
// const handleAvatarSuccess: UploadProps['onSuccess'] = (
//   response,
//   uploadFile
// ) => {
//   imageUrl.value = URL.createObjectURL(uploadFile.raw!)
//   console.log(imageUrl.value, '图片')
// }
const avatarFileChange = (e: { raw?: any }) => {
  avatarFile.value = e
  imageUrl.value = URL.createObjectURL(e.raw)
  // imgs1.value.push(imageUrl.value)
  console.log(imageUrl.value, '图片')
  let img = new Image()
  img.src = imageUrl.value

  // 等待图片加载完成
  img.onload = async () => {
    // 创建canvas元素
    let canvas = document.createElement('canvas')
    let ctx: any = canvas.getContext('2d')

    // 设置canvas尺寸
    let width = img.width
    let height = img.height
    let scale = width / height
    let maxWidth = 800
    let maxHeight = 800 / scale
    canvas.width = maxWidth
    canvas.height = maxHeight

    // 绘制图片
    ctx.drawImage(img, 0, 0, maxWidth, maxHeight)

    // 将canvas转换为base64格式的图片
    let base64 = canvas.toDataURL('image/jpeg', 0.8)
    // 将base64格式的图片转换为File对象
    let file = dataURLtoFile(base64)
    avatarFile.value.raw = file
    let { data } = await reqUpload(avatarFile.value.raw)
    if (data.code != 1) {
      return ElMessage.error('上传失败！')
    }
    const aa = 'http://wuj.vunmi.net/' + data.data.url
    imgs.value.push(aa)
    console.log(imgs, '图片地址')
    // imageUrl.value = data.data.url
  }
}
const beforeAvatarUpload: UploadProps['beforeUpload'] = async (
  rawFile: any
) => {
  if (
    rawFile.type !== 'image/jpeg' &&
    rawFile.type !== 'image/png' &&
    rawFile.type !== 'image/jpg' &&
    rawFile.type !== 'image/bmp'
  ) {
    ElMessage.error('图片格式不对!')
    return false
  } else if (rawFile.size / 1024 / 1024 > 1) {
    ElMessage.error('图片大小不能超过 1MB!')
    return false
  }

  // rawFile = compressImage(rawFile)
  // let res = await reqUpload(rawFile)
  // imageUrl.value = res.data.data.uri

  // console.log(res)

  return false
}

const centerDialogVisible = ref(false)
const centerDialogVisible1 = ref(true)
function center() {
  centerDialogVisible.value = false
  centerDialogVisible1.value = true
}
const total = ref(0)
const names = ref({
  page: 1,
  page_size: 3
})
const data = ref()
const getlists = () => {
  getlistsApi(names.value).then(res => {
    total.value = res.data.data.total
    data.value = res.data.data.data
    // data.value.forEach((item: any) => {
    //   item.images = JSON.parse(item.images)
    // })
    console.log(res)
  })
}
getlists()
const del = (id: any) => {
  gerdelApi({ id: id }).then(res => {
    // console.log(res)
    ElMessage({ message: '删除成功', type: 'success' })
    getlists()
  })
}
const like = (id: any) => {
  getlikeApi({ id: id }).then(res => {
    console.log(res)
    getlists()
  })
}
const pageSize = ref(3)
const nextclick = () => {
  names.value.page++
  getlists()
}
const prevclick = () => {
  names.value.page--
  getlists()
}
const change1 = (e: any) => {
  console.log(e)
  names.value.page = e
  getlists()
}
</script>

<style lang="scss" scoped>
.cen_time {
  width: 15%;
  height: 200px;
  text-align: right;
  margin-right: 20px;
}

.cen_textss {
  width: 100%;
  font-size: 18px;
  line-height: 35px;
  // line-height: 45px;
}
.kai {
  display: flex;
  justify-content: end;
  cursor: pointer;
  color: red;

  div {
    width: 50px;
    // background: olive;
  }
}
.e_rig {
  width: 16px;
  height: 16px;
}
.szan {
  display: flex;
  align-items: center;

  img {
    cursor: pointer;
    width: 25px;
    height: 25px;
  }
}
.rq {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  margin-top: 100px;
  img {
    width: 228px;
    height: 226px;
  }
}
.mask {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  z-index: 999;
  .content::-webkit-scrollbar {
    display: none;
  }

  .content {
    width: 680px;
    height: 400px;
    overflow: auto;
    margin: 100px auto;
    padding: 32px 40px 35px;
    background-color: #fff;
    .con {
      display: flex;
      flex-direction: column;
      align-items: center;
      // text-align: center;
      .tit {
        font-size: 18px;
        font-weight: 600;
        color: #1a1a1a;
        margin: 60px 0 45px;
      }
      .wzdl {
        font-size: 16px;
        font-weight: 500;
        line-height: 40px;
        text-align: center;
        width: 157px;
        height: 40px;
        border-radius: 60px;
        background: #e02020;
        color: #ffffff;
      }
    }
    .btn {
      display: flex;
      margin-top: 48px;
      .bt {
        text-align: center;
        line-height: 40px;
        width: 96px;
        height: 40px;
        font-size: 16px;
        font-weight: 500;
        border-radius: 60px;
        background: #e02020;
        color: #fff;
      }
      .qx {
        margin: 0 32px 0 355px;
        background: #f5f5f5;
        color: #999999;
      }
    }
    .fw {
      display: flex;
      margin-top: 34px;
      .fw_lef {
        font-size: 16px;
        font-weight: 600;
        color: #1a1a1a;
        margin-right: 39px;
        margin-top: 5px;
      }
    }
    .sc {
      display: flex;
      margin-top: 31px;
      .sc_rig {
        .dj {
          cursor: pointer;
          text-align: center;
          width: 88px;
          height: 32px;
          line-height: 32px;
          border-radius: 4px;
          /* 党建主色 */
          background: #e02020;
          font-size: 14px;
          font-weight: 500;
          color: #ffffff;
        }
        .w {
          margin-top: 24px;
          width: 124px;
          font-size: 14px;
          color: #1a1a1a;
          line-height: 32px;
        }
      }
      .sc_lef {
        width: 155px;
        height: 155px;
        margin-right: 16px;
        background: rgba(224, 32, 32, 0.1);
        text-align: center;
        line-height: 205px;
        .el-icon {
          font-size: 60px;
          color: #e02020;
        }
      }
    }
    .con_title {
      display: flex;
      justify-content: space-between;
      font-size: 18px;
      font-weight: 600;
      color: #1a1a1a;
    }
    .el-textarea {
      margin-top: 32px;
      height: 300px;
      background: #f5f5f5;
      ::v-deep .el-textarea__inner {
        height: 300px !important;
        font-size: 20px;
        background: #f5f5f5;
      }
    }
    .img {
      display: flex;
      flex-wrap: wrap;
      .bbb {
        position: relative;
        margin: 32px 20px 0 0;
        width: 155px;
        height: 155px;
      }
      .imgs {
        // margin: 32px 20px 0 0;
        width: 155px;
        height: 155px;
      }
      .aaa {
        background-color: rgba(153, 153, 153, 0.5);
        position: absolute;
        top: 0;
        right: 0;
      }
      .bbb:nth-child(4n) {
        margin-right: 0;
      }
    }
  }
  .asd {
    height: 300px;
  }
}
.interspace {
  position: relative;
  height: 1200px;
  margin-top: -18px;
  margin-left: -16px;
  width: 800px;
  background-color: #fff;
  padding: 32px 0 300px 0;
  .con_bom {
    position: absolute;
    bottom: 100px;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    .cc {
      width: 60px;
      margin-top: 8px;
    }
  }
  .main {
    padding: 0 49px 0 43px;
    .m_item {
      .br {
        margin: 10px 0 22px 0;
        border-bottom: 1px solid #e5e5e5;
      }
      .it_bom {
        display: flex;
        justify-content: space-between;
        margin-top: 20px;
        .m_le {
          display: flex;
          .le {
            margin-left: 5px;
          }
        }
      }
      .it_img {
        margin-left: 52px;
        img {
          margin-top: 27px;
          width: 140px;
          height: 140px;
          margin-right: 11px;
          object-fit: cover;
          // background-color: blue;
          border-radius: 8px;
        }
      }
      .it_top {
        display: flex;
        .p_rig {
          width: 656px;
          font-size: 14px;
          color: #1a1a1a;
          overflow: hidden; /* 隐藏溢出内容 */
          text-overflow: ellipsis; /* 当文本溢出时，使用省略号表示 */
          .zk {
            font-size: 16px;
            background-color: #fff;
            text-align: right;
            color: #e02020;
          }
          .text {
            display: -webkit-box; /* 启用弹性盒子布局 */
            -webkit-line-clamp: 3; /* 限制显示2行文本 */
            -webkit-box-orient: vertical; /* 垂直方向排列子元素 */
            overflow: hidden; /* 隐藏溢出内容 */
            text-overflow: ellipsis; /* 当文本溢出时，使用省略号表示 */
          }
        }
        .p_lef {
          .f_p {
            display: flex;
            font-size: 13px;
            font-weight: 500;
            color: #1a1a1a;
            .ri {
              width: 19px;
              font-size: 18px;
              font-weight: normal;
            }
            .yue {
              width: 39px;
              margin: 3px 0 0 6px;
            }
          }
          .f_m {
            font-size: 13px;
            font-weight: 500;
            line-height: 24px;
            margin-left: 10px;
            color: #767676;
          }
        }
      }
    }
    .fb {
      width: 133px;
      height: 40px;
      line-height: 40px;
      text-align: center;
      margin-left: 579px;
      font-size: 16px;
      font-weight: 500;
      border-radius: 60px;
      color: #fff;
      background: #e02020;
      margin-bottom: 24px;
    }
  }
}
</style>
